package com.yunji.common.model.dto;

import com.alibaba.fastjson2.annotation.JSONField;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.enums.LoginTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.common.core.domain.model
 * @Title: LoginInfo
 * @Description: 缓存登录用户共同信息
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-04-17 15:49:10      V1.0        HuaAo       新建类
 */
@Data
public class LoginInfo implements UserDetails {
    @ApiModelProperty("登录类型")
    private LoginTypeEnum loginType;

    @ApiModelProperty("用户ID")
    private Long userId;

    @ApiModelProperty("所属单位ID")
    private Long belongId;

    @ApiModelProperty("所属单位名称")
    private String belongName;

    @ApiModelProperty("所属网点ID")
    private Long siteId;

    @ApiModelProperty("所属代理ID")
    private Long agentId;

    @ApiModelProperty("用户名称/openid/手机号码")
    private String username;

    @ApiModelProperty("用户昵称")
    private String nickname;

    @ApiModelProperty("真实信息")
    private String realName;

    private String openId;

    @ApiModelProperty("手机号码")
    private String phone;

    @ApiModelProperty("用户类型")
    private String userType;

    private List<SysRoleDTO> roles;
    /**
     * 用户唯一标识
     */
    private String tokenKey;

    private String tokenValue;

    private String password;

    private Date createTime;

    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 权限列表
     */
    private Set<String> permissions;



    public boolean isAdmin() {
        return this.userId != null && 1L == this.userId && UserType.PLATE.getCode().equals(this.getUserType());
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }
}
